#  chalyx.py - Main chalyx object
#  Copyright (C) 2005-2007 Sham Chukoury <eleusis@xmms.org>
#
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2.1 of the License, or (at your option) any later version.
#
#  This library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Lesser General Public License for more details.

from Chalyx.sepals import *
import sys
import os

def get_config_dir_path(service=None):
	"""Get path to configuration file"""
	homedir = os.path.expanduser("~/")
	if service == "xmms2":
		try:
			import xmmsclient
		except ImportError:
			print "Cannot import xmmsclient - falling back to freedesktop config path"
		else:
			confdir = xmmsclient.userconfdir_get()
			return os.path.join(confdir, "clients")
	confdir = os.getenv("XDG_CONFIG_HOME")
	if confdir == None:
		if sys.platform == "darwin":
			confdir = os.path.join(homedir, "Library")
		else:
			confdir = os.path.join(homedir, ".config")
	return confdir

def xmms2_service(connClsData, name, cfgHandler):
	"""Starts the xmms2 service"""
	try:
		from services import xmms2
	except ImportError:
		print "Cannot initialise XMMS2 service - XMMS2 python bindings not available"
		return None
	return xmms2.XMMS2Service(connClsData, name, cfgHandler)

def mpd_service(connClsData, name, cfgHandler):
	"""Starts the mpd service"""
	try:
		from services import mpd
	except ImportError:
		print "Cannot initialise MPD service - mpdclient2 not available?"
		return None
	return mpd.MPDService(connClsData, name, cfgHandler)

class Chalyx:
	initServ = {"xmms2": xmms2_service,
				"mpd": mpd_service}
	def __init__(self, connClsData, service, cfgHandler, name="Chalyx"):
		self.cfgHandler = cfgHandler
		self.name = name
		self.service = self.initServ[service](connClsData, name, cfgHandler)
		if not self.service:
			raise RuntimeWarning("Unable to initialise service")
		self._library = Library(self.service)
		self._player = Player(self.service, self.library)
		self._playlist = Playlist(self.service, self.library)

	def _get_library(self):
		return self._library
	library = property(_get_library)
	
	def _get_player(self):
		return self._player
	player = property(_get_player)
	
	def _get_playlist(self):
		return self._playlist
	playlist = property(_get_playlist)

	def service_connect(self, address = None):
		return self.service.s_connect(address)

	def service_disconnect(self):
		return self.service.s_disconnect()

	def swap_connector(self, newconn, service):
		# completely useless method atm
		# might be useful later!
		self.service.disconnect()
		del self.service
		self.service = self.initServ[service](newconn, self.name, self.cfgHandler)
